package com.huli.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huli.mapper.NursingProjectMapper;
import com.huli.mapper.OrganizationUserMapper;
import com.huli.pojo.NursingProject;
import com.huli.pojo.SysOrganization;
import com.huli.service.NursingProjectService;
import com.huli.vo.OrganizationUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    // 注入NursingProjectMapper
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    // 注入OrganizationUserMapper
    @Autowired
    private OrganizationUserMapper organizationUserMapper;
    // 根据组织用户Vo查询护理项目列表
    @Override
    public OrganizationUserVo nursingProjectList(OrganizationUserVo organizationUserVo) {
        // 设置分页参数
        PageHelper.startPage(organizationUserVo.getPageNum(), organizationUserVo.getPageSize());
        // 查询护理项目列表
        List<NursingProject> nursingProjectList = nursingProjectMapper.nursingProjectList(organizationUserVo);
        // 遍历护理项目列表，设置组织名称
        for (NursingProject nursingProject : nursingProjectList) {
            SysOrganization sysOrganization = organizationUserMapper.selectOrganization(nursingProject.getOrgId());
            nursingProject.setOrgName(sysOrganization.getOrgName());
        }
        // 将护理项目列表封装到PageInfo中
        PageInfo<NursingProject> nursingProjectPageInfo = new PageInfo<>(nursingProjectList);
        // 返回组织用户Vo
        return new OrganizationUserVo(nursingProjectPageInfo.getTotal(), nursingProjectPageInfo.getList());
    }

    // 更新护理项目
    @Override
    public void updateNursingProject(NursingProject nursingProject) {
        // 根据id查询护理项目
        NursingProject nursingProjectById = nursingProjectMapper.nusingProjectById(nursingProject.getId());
        // 根据组织id查询组织
        SysOrganization sysOrganizationById = organizationUserMapper.organizationById(nursingProjectById.getOrgId());
        // 设置组织名称
        nursingProjectById.setOrgName(sysOrganizationById.getOrgName());
        // 更新护理项目
        nursingProjectMapper.updateNursingProject(nursingProject);
    }

    @Override
    public void addNursingProject(NursingProject nursingProject) {
        nursingProjectMapper.addNursingProject(nursingProject);
    }

    @Override
    public NursingProject selectNursingProjectById(Long id) {
        return nursingProjectMapper.selectNursingProjectById(id);
    }

    @Override
    public List<NursingProject> findAll() {
        return nursingProjectMapper.nuringProjectListName();
    }
}
